package com.example.pixelyear.ui.act

import android.graphics.Typeface
import android.os.Bundle
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.widget.TextView
import com.example.pixelyear.R
import com.example.pixelyear.ui.base.BaseActivity
import com.example.pixelyear.utils.*
import com.example.pixelyear.utils.extension.gone
import com.example.pixelyear.utils.extension.setOnDoubleClickListener
import com.example.pixelyear.utils.extension.visi
import kotlinx.android.synthetic.main.activity_about_me.*
import kotlinx.android.synthetic.main.include_title.*
import java.util.*

/**
 * 关于我们界面
 */
class AboutMeActivity : BaseActivity() {
    private var showAgg = 0

    override fun getLayoutResID(): Int {
        return R.layout.activity_about_me
    }

    override fun initView(bundle: Bundle?) {
        if (!YxsSpUtils.getBoolean(Constant.OPEN_SENIOR_EDIT_COLOR, false)
            && getRandom() == 8
        ) {
            eggs()
        } else {
            showAgg = intent?.getBundleExtra(YxsDisplay.beanName)?.getInt("random", 0) ?: 0
            if (showAgg == 8)
                eggs()
            else
                normal()
        }
    }

    override fun initData() {

    }

    override fun initListener() {
        yxstitle_Imgback.setOnDoubleClickListener {
            finish()
        }
    }

    private fun getRandom(): Int {
        val random = Random()
        return random.nextInt(50)
    }

    private fun eggs() {
        AboutMe_AppVersion.gone()
        yxstitle_Imgback.setImageResource(R.mipmap.back_gold)
        yxstitle_TvTitle.text = "彩彩彩彩彩彩彩彩蛋"
        yxstitle_TvTitle.setTextColor(UIUtils.getColor(R.color.color_gold))
        yxstitle_TvTitle.typeface = Typeface.defaultFromStyle(Typeface.BOLD)

        AboutMe_AppName.text = "哇 金色传说"
        AboutMe_AppName.setTextColor(UIUtils.getColor(R.color.color_gold))
        AboutMe_AppName.typeface = Typeface.defaultFromStyle(Typeface.BOLD)

        AboutMe_Msg.text = UIUtils.getString(R.string.ge_d_lj)
        AboutMe_Msg.setTextColor(UIUtils.getColor(R.color.color_gold))
        AboutMe_Msg.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        AboutMe_Msg.gone()
        alpha(AboutMe_Msg) {
            alpha(AboutMe_ClickUp)
        }
        AboutMe_Msg.setOnClickListener {
            alpha(AboutMe_State) {
                alpha(AboutMe_Start)
            }
        }

        AboutMe_Start.setOnClickListener {
            showDialog()
        }

        YxsSpUtils.putBoolean(Constant.OPEN_SENIOR_EDIT_COLOR, true)
    }

    private fun normal() {
        yxstitle_TvTitle.text = "关于App"
        AboutMe_AppVersion.text = "v " + Utils.getVersionName(mActivity)
        if (YxsSpUtils.getBoolean(Constant.OPEN_SENIOR_EDIT_COLOR, false)) {
            AboutMe_Msg.text = UIUtils.getString(R.string.about_me_text_not_egg)
        } else AboutMe_Msg.text = UIUtils.getString(R.string.about_me_text_have_egg)
    }

    private fun alpha(view: TextView, end: (() -> Unit?)? = null) {
        view.visi()
        val alpha = AlphaAnimation(0f, 1f)
        alpha.duration = 2000
        view.startAnimation(alpha)
        alpha.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {

            }

            override fun onAnimationEnd(animation: Animation?) {
                end?.invoke()
                if (view == AboutMe_Start)
                    showDialog()
            }

            override fun onAnimationStart(animation: Animation?) {
            }
        })
    }

    private fun showDialog() {
        if (showAgg == 8)
            TipsDialogUtils.showSureDialog(mActivity, content = "恭喜你，又看了一遍～") {}
        else
            TipsDialogUtils.showSureDialog(mActivity, content = "恭喜你开启了高级编辑颜色模式，只有百分之一的几率哦，所有颜色都可以编辑，详见颜色编辑界面！") {}
    }

}
